<template>
  <div class="friend-circle-log">
    <div class="friend-circle-log-title">
      <p>朋友圈日志</p>
      <i class="el-icon-close" @click="$parent.toggleLogPage"></i>
    </div>
    <el-table class="box-table" :data="tableData[currentPage - 1]" border stripe height="100%">
      <el-table-column
        label="任务类型"
        prop="MsgType"
        :filters="msgTypes"
        :filter-method="filterMsgType"
      ></el-table-column>
      <el-table-column label="所属微信">
        <template slot-scope="scope">
          <span
            v-text="wechatsNameMap[scope.row.WeChatId] || scope.row.WeChatId"
          ></span>
        </template>
      </el-table-column>
      <el-table-column label="任务时间">
        <template slot-scope="scope">
          <span v-text="$options.filters.timeFilter(scope.row.TaskId)"></span>
        </template>
      </el-table-column>
      <el-table-column
        label="任务结果"
        prop="result"
        width="100px"
        :formatter="resultFormatter"
        :filters="results"
        :filter-method="filterResult"
      ></el-table-column>
    </el-table>
    <!-- 分页 -->
    <el-pagination
      style="margin-top: 10px"
      :current-page="currentPage"
      :page-sizes="[50, 100, 150, 200]"
      :page-size="pageSize"
      :pager-count="7"
      layout="total, sizes, prev, pager, next, jumper"
      :total="totalPage"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    ></el-pagination>
  </div>
</template>

<script>
import { mapGetters } from 'vuex'

export default {
  name: 'FriendCircleLog',
  data() {
    return {
      dialogTableVisible: false,
      msgTypes: [
        { text: '点赞', value: 'CircleLikeTask' },
        { text: '评论', value: 'CircleCommentReplyTask' },
        { text: '删除评论', value: 'CircleCommentDeleteTask' },
        { text: '删除朋友圈', value: 'DeleteSNSNewsTask' },
        { text: '发朋友圈', value: 'PostSNSNewsTask' }
      ],
      results: [
        { text: '成功', value: 1 },
        { text: '失败', value: 2 },
        { text: '执行中', value: 0 }
      ],
      currentPage: 1, // 当前页
      pageSize: 50 // 每页消息数
    }
  },
  computed: {
    ...mapGetters({
      wechatsNameMap: 'conversation/wechatsNameMap',
      currentWeChatId: 'conversation/currentWeChatId',
      friendCircleLog: 'nedb/friendCircleLog'
    }),
    // 好友总数
    totalPage() {
      return this.friendCircleLog.length
    },
    // 填充表格数据
    tableData() {
      // 分页 每页50
      const newLogs = []
      const size = this.pageSize
      for (let i = 0; i < this.friendCircleLog.length; i += size) {
        newLogs.push(this.friendCircleLog.slice(i, i + size))
      }
      return newLogs
    }
  },
  watch: {
    currentWeChatId() {
      this.currentPage = 1
    }
  },
  methods: {
    // 修改pageSize
    handleSizeChange(val) {
      this.pageSize = val
    },
    // 修改当前页
    handleCurrentChange(val) {
      this.currentPage = val
    },
    // 格式化结果
    // eslint-disable-next-line no-unused-vars
    resultFormatter(row, column) {
      const resultMap = {
        0: '执行中',
        1: '成功',
        2: '失败'
      }
      return resultMap[row.result]
    },
    // 任务类型过滤器
    filterMsgType(value, row, column) {
      const property = column['property']
      return row[property] === value
    },
    // 任务结果过滤器
    // eslint-disable-next-line no-unused-vars
    filterResult(value, row, column) {
      return row.result === value
    }
  }
}
</script>

<style lang="scss" scoped>
.friend-circle-log {
  width: 100%;
  height: 100%;
  padding: 10px;

  display: flex;
  flex-direction: column;
  align-items: center;

  .friend-circle-log-title {
    width: 100%;
    padding: 10px 0;
    display: flex;
    justify-content: space-between;
    i {
      cursor: pointer;
    }
  }

  ::v-deep .box-table {
    z-index: 2;
    .el-table__header-wrapper {
      // min-height: 60px;
      min-height: 40px;
      display: flex;
      align-items: center;
      background-color: #ff9f000d;

      .el-table__header {
        th {
          background-color: #ff9f000d;
          padding: 0;
          border-bottom: none;
        }
      }
    }
  }
}
</style>
